import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Button, Icon, notification, Select} from "antd";
import lodash from 'lodash';

const {Option} = Select;

class Toolbar extends PureComponent {
    handleExport = () => {
        const {dataSource} = this.props;
        const fileName = "项目数据汇总";
        document.getElementById("fileName").value = fileName;
        if (dataSource && dataSource.length > 0) {
            const headers = [];
            headers.push("客户");
            headers.push("应收运费(RMB)");
            headers.push("应收运费(USD)");
            headers.push("应收杂费(RMB)");
            headers.push("应收杂费(USD)");
            headers.push("应收合计款(RMB)");
            headers.push("应收合计款(USD)");
            headers.push("供应商");
            headers.push("应付运费(RMB)");
            headers.push("应付运费(USD)");
            headers.push("应付杂费(RMB)");
            headers.push("应付杂费(USD)");
            headers.push("应付合计款(RMB)");
            headers.push("应付合计款(USD)");
            headers.push("财务归属");
            headers.push("营业利润(RMB)");
            headers.push("业务类型");
            const newDataList = [];
            dataSource.forEach(item => {
                const newItem = {
                    "客户": item['client'],
                    "应收运费(RMB)": Number(item['incomeTransportFeeRmb']).toFixed(2),
                    "应收运费(USD)": Number(item['incomeTransportFeeUsd']).toFixed(2),
                    "应收杂费(RMB)": Number(item['incomeOtherFeeRmb']).toFixed(2),
                    "应收杂费(USD)": Number(item['incomeOtherFeeUsd']).toFixed(2),
                    "应收合计款(RMB)": Number(item['totalIncomeRmb']).toFixed(2),
                    "应收合计款(USD)": Number(item['totalIncomeUsd']).toFixed(2),
                    "供应商": item['carrier'],
                    "应付运费(RMB)": Number(item['payTransportFeeRmb']).toFixed(2),
                    "应付运费(USD)": Number(item['payTransportFeeUsd']).toFixed(2),
                    "应付杂费(RMB)": Number(item['payOtherFeeRmb']).toFixed(2),
                    "应付杂费(USD)": Number(item['payOtherFeeUsd']).toFixed(2),
                    "应付合计款(RMB)": Number(item['totalPayRmb']).toFixed(2),
                    "应付合计款(USD)": Number(item['totalPayUsd']).toFixed(2),
                    "财务归属": item['orgName'],
                    "营业利润(RMB)": Number(item['currentAssets']).toFixed(2),
                    "业务类型": item['businessType']
                };
                newDataList.push(newItem);
            });
            document.getElementById("headers").value = lodash.join(headers, ',');
            document.getElementById("rows").value = JSON.stringify(newDataList);
            document.getElementById("exportFile").submit();
            notification.success({
                message: '请等待文件导出'
            });
        } else {
            notification.error({
                message: '没有数据可以导出'
            });
        }
    };

    changeBank = value => {
        document.getElementById("bankInfoId").value = value;
    };

    render() {
        const {filter = {}, bankList} = this.props;
        return (
            <div className="actions">
                {/*银行账号：
                <Select style={{width: 400}} allowClear={true} onChange={this.changeBank}>
                    {bankList.map(bank => (
                        <Option value={bank.id} key={bank.mark}>
                            {bank.mark + " 人民币账户" + bank.rmbAccount + " 美元账户" + bank.usdAccount}
                        </Option>
                    ))}
                </Select>*/}
                <Button style={{marginLeft: "10px"}} onClick={this.handleExport}><Icon type="export"/>导 出</Button>
                <form
                    id="exportFile"
                    method="post"
                    target="_blank"
                    action="/api/orderAnalysis/commonExport"
                >
                    <input
                        id="fileName"
                        name="fileName"
                        type="hidden"
                    />
                    <input
                        id="rows"
                        name="rows"
                        type="hidden"
                    />
                    <input
                        id="headers"
                        name="headers"
                        type="hidden"
                    />
                </form>
            </div>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.orderProfit.list,
        bankList: state.bankInfo.list.dataSource,
        dataSource: state.orderProfit.list.dataSource
    };
};

export default connect(mapStateToProps)(Toolbar);
